package fr.uparis10.miage.m1p1.menu.employe;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import fr.uparis10.miage.m1p1.config.Configurator;
import fr.uparis10.miage.m1p1.core.Employe;
import fr.uparis10.miage.m1p1.global.LogFactory;
import fr.uparis10.miage.m1p1.menu.MenuExecutor;

public class ShowEmployeExecutor implements MenuExecutor {
	private Logger log = LogFactory.getInstance().getLogger(
			ShowEmployeExecutor.class.getName());

	public void execute() {
		// TODO ask the employee nr. and search it in the organization
		byte[] buf = new byte[100];
		System.out.print("Employee ID? (0 - cancel): ");
		int employeId;
		while (true) {
			try {
				System.in.read(buf);
				String tmp = (new String(buf)).trim();
				employeId = Integer.parseInt(tmp);
			} catch (IOException exc) {
				log.log(Level.SEVERE, "Ooops...", exc);
				return;
			} catch (NumberFormatException exc) {
				log.log(Level.WARNING, "Invalid number", exc);
				continue;
			}
			break;
		}
		if (employeId == 0) {
			return;
		}
//		ObjectJ entr = Configurator.getInstance().getObjectJ();
		// TODO: change this logic to get everything from ObjectJ
		Employe empl = Configurator.getInstance().getEmployeMap().get(employeId);
		if (empl == null) {
			System.out.println("No such employe");
			return;
		}
		System.out.println();
		System.out.println(empl);
	}

}
